/** 
 * @Title:  UserAcountFragment.java 
 * @author:  Abtswiath丶lxy
 * @data:  2016年6月15日 上午10:35:18 <创建时间>
 * 
 * @history：<以下是历史记录>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月15日 上午10:35:18 <修改时间>
 * @log: <修改内容>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月15日 上午10:35:18 <修改时间>
 * @log: <修改内容>
 */
package com.sunmi.usercenter.ui.activity.fragment;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.sunmi.usercenter.Constant;
import com.sunmi.usercenter.R;
import com.sunmi.usercenter.bean.FmInfo;
import com.sunmi.usercenter.bean.Result;
import com.sunmi.usercenter.bean.Shop;
import com.sunmi.usercenter.bean.rpc.ReqVerifyPassword;
import com.sunmi.usercenter.utils.MD5Utils;
import com.sunmi.usercenter.utils.Validator;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.Header;

import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditPwdDialog;
import sunmi.sunmiui.dialog.LoadingDialog;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author Abtswiath丶lxy
 * @versionCode 1 <每次修改提交前+1>
 */
public class UserAcountFragment extends SuperFragment implements OnClickListener{

	private View mView;
	private RelativeLayout mRelBack,mRelUpdatePassword;
	private TextView mTxtUid,mTxtPhone,mTxtCode;
	private Button mBtnLoginOut;
	private RelativeLayout mLinUid;
	private Shop mShop;
	private EditPwdDialog mEditPwdDialog;
	private LoadingDialog mLoadingDialog;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		verifyLogin();
		initView();
		fillData();
	}
	
	private void verifyLogin(){
		mShop = mCaCheModel.getShop();
		if(mShop==null){
			openFM(new FmInfo(LoginFragment.class, null));
			onDestroy();
		}
	}

	/**
	 * 填充数据
	 */
	private void fillData() {
		if(mShop!=null){
			mTxtUid.setText(mShop.getuId()+"");
			String userName = mShop.getUserName();
			if(Validator.isEmail(userName)){
				String string = "";
				for (int i = 0; i < (userName.split("@")[0].length()-3); i++) {
					string = string+"*";
				}
				mTxtPhone.setText(userName.substring(0,3) +string +"@" + userName.split("@")[1]);
				mTxtCode.setText(getString(R.string.binding_email));
			}else{
				mTxtPhone.setText(userName.substring(0,3) + "*****" + userName.substring(8, userName.length()));
				mTxtCode.setText(getString(R.string.binding_phone));
			}
		}
		mLinUid.setVisibility(View.GONE);
	}

	/**
	 * 初始化
	 */
	private void initView() {
		mActivity.SetStatusColorFE542B();
		mRelBack = (RelativeLayout) mView.findViewById(R.id.back);
		mTxtUid = (TextView) mView.findViewById(R.id.txt_user_id);
		mTxtPhone = (TextView) mView.findViewById(R.id.txt_phone);
		mTxtCode = (TextView)mView.findViewById(R.id.txt_code);
		mRelUpdatePassword = (RelativeLayout) mView.findViewById(R.id.lin_update_password);
		mBtnLoginOut = (Button) mView.findViewById(R.id.btn_login_out);
		mLinUid = (RelativeLayout) mView.findViewById(R.id.liner_user_id) ;
		((TextView)mView.findViewById(R.id.title)).setText(getString(R.string.this_account));
		mRelUpdatePassword.setOnClickListener(this);
		mBtnLoginOut.setOnClickListener(this);
		mRelBack.setOnClickListener(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		return mView = inflater.inflate(R.layout.fragment_user_account, container, false);
	}
	
	private void LoginOut(){


		mEditPwdDialog = DialogCreater.createEditPwdDialog(getActivity(),getString(R.string.cancel), getString(R.string.exit_login), getString(R.string.please_input_your_password), new OnClickListener() {
			@Override
			public void onClick(View v) {
				mEditPwdDialog.dismiss();
			}
		}, new OnClickListener() {
			@Override
			public void onClick(View v) {
				String psd = mEditPwdDialog.getText();
				if(psd.length()<6){
					mEditPwdDialog.showError(getString(R.string.password_try_input));
					return;
				}
				ReqVerifyPassword reqVerifyPassword = new ReqVerifyPassword();
				reqVerifyPassword.setPassword(MD5Utils.md5Password(mShop.getUserName(), psd));
				reqVerifyPassword.setuId(mShop.getuId());
				mHttpController.verifyPassword(reqVerifyPassword, new AsyncHttpResponseHandler() {

					@Override
					public void onStart() {
						super.onStart();
						mLoadingDialog = DialogCreater.createLoadingDialog(getActivity(),getString(R.string.waitting));
						mLoadingDialog.show();
					}

					@Override
					public void onSuccess(int i, Header[] headers, byte[] bytes) {
						String json = new String(bytes);
						Gson gson = new Gson();

						try {
							Result result = gson.fromJson(json, Result.class);
							if(result!=null&&result.code==1){
								Toast.makeText(getActivity(), getString(R.string.exit_success), Toast.LENGTH_SHORT).show();
								mCaCheModel.clearShop();
								mEditPwdDialog.dismiss();
								openFM(new FmInfo(LoginFragment.class, null));
							}else{
								mEditPwdDialog.showError(getString(R.string.password_try_input));
							}
						} catch (Exception e) {
							Toast.makeText(getActivity(), getString(R.string.req_error), Toast.LENGTH_SHORT).show();
						}
						mLoadingDialog.dismiss();
					}
					@Override
					public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
						Toast.makeText(getActivity(), getString(R.string.req_error), Toast.LENGTH_SHORT).show();
						mLoadingDialog.dismiss();
					}
				});
			}
		});
		mEditPwdDialog.show();

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				mEditPwdDialog.showSoftInput();
			}
		}, 998);

//		mExitDialog = ExitDialog.createDialog(getActivity(), new ExitLintener() {
//
//			@Override
//			public void exitLogin(String password) {
//				mCaCheModel.clearShop();
////				mCaCheModel.clearUserPasswordAndUsername();
//				openFM(new FmInfo(LoginFragment.class, null));
//			}
//			@Override
//			public void cancel() {
//				mExitDialog.dismiss();
//			}
//		}, mShop);
//		mExitDialog.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lin_update_password:
			openFM(new FmInfo(UserUpdatePasswordFragment.class, null));
			break;
		case R.id.btn_login_out:
			LoginOut();
			break;
		case R.id.back:
			onBack();
		default:
			break;
		}
	}
}
